﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.OleDb;
using System.Text;
using System.Xml;

public partial class news : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        CreateHtml html = new CreateHtml();
        html.CreateHtmlAuto();
        if(!Page.IsPostBack)
        {
            //GetHeadMenuHtml();
            GetNewsContent();
        }
    }
    protected void GetNewsContent()
    {
        StringBuilder sb = new StringBuilder();
        XmlDocument doc = new XmlDocument();
        doc.Load(Server.MapPath("~/App_Data/HomePage.xml"));
        XmlElement root = doc.DocumentElement;

        string siteContent = "";
        siteContent = root.SelectSingleNode("htmlNewsSiteId").InnerText;

        string[] sites = siteContent.Split(';');
        int n = 1;
        string[] siteCurrent = new string[2];
        foreach(string s in sites)
        {
            siteCurrent = s.Split(',');
            string id, name;
            id = siteCurrent[0];
            name = siteCurrent[1];
            try
            {
                OleDbDataReader read = GetNews(id);//一个新闻站点开始，s.Substring(0, s.IndexOf(','))                
                sb.Append("<div class='newsColumn'><div id='newsHead" + n.ToString() + "' class='headNews'>" + name + "</div>");
                sb.Append("<div id='newsNav" + n.ToString() + "' class='newsDiv'><ul>");
                while(read.Read())
                {
                    sb.Append("<li><a class='newstitle' href='" + read[0].ToString() + "' target='_blank'>" +
                           read[1].ToString() + "</a><span class='newsdate'>" + read[2].ToString() + "</span></li>");
                }
                read.Close();
                sb.Append("</ul></div></div>");//一个新闻站点结束
                n++;
            }
            catch(OleDbException oex)
            {
                sb.Append("</ul></div></div>");
                string aaaa = oex.Message;
                n++;
            }
        }
        LNews.Text = sb.ToString();       
    }
    protected void GetHeadMenuHtml()
    {
        StringBuilder sb = new StringBuilder();
        sb.Append("<ul>");
        string command = "select HtmlKind, HtmlAddress, KindName from CurrentHtml where HtmlAddress is not null order by MenuOrder";
        OleDbConnection con = new OleDbConnection(GetConfigContent.ConnectionStringNews);
        OleDbCommand cmd = new OleDbCommand(command, con);
        con.Open();
        OleDbDataReader r = cmd.ExecuteReader();
        while(r.Read())
        {
            if(r[0].ToString() != "index")//如果是本分类，用现在即将生成的最新文件名，生成菜单
            {
                sb.Append("<li><nobr><a href='htmlNews/" + r[1].ToString() + "' target='_blank'>" + r[2].ToString() + "</a></nobr></li>");
            }
        }
        r.Close();
        con.Close();
        sb.Append("<li><a href='index.aspx' class='currentMenu' target='_blank'><nobr>首　页</nobr></a></li></ul>");
        //sb.Append("</ul>");
        LMenu.Text = sb.ToString();
    }
    protected OleDbDataReader GetNews(string siteId)
    {
        OleDbConnection con = new OleDbConnection(GetConfigContent.ConnectionStringNews);
        OleDbCommand cmd = new OleDbCommand("SELECT [link],[title],[pubDate] FROM [RssNews] WHERE [siteId] = " + siteId, con);
        con.Open();
        return cmd.ExecuteReader(CommandBehavior.CloseConnection);
    }
    //protected void Button1_Click(object sender, EventArgs e)
    //{
    //    CreateHtml html = new CreateHtml();
    //    html.CreateHtmlAuto();
    //}
}
